﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraGrid.Columns;
using ElingAppSystemCtrls;
using DevExpress.XtraEditors.Repository;
using System.Collections;
using NetDiskPRJ.Model;
using Newtonsoft.Json;
using NetDiskPRJ.Common;

namespace NetDiskPRJ.Base
{
    public partial class AdvCommonQueryDlgForm : Base.DlgForm
    {
        /// <summary>
        /// Grid列数组
        /// </summary>
        private GridColumnCollection GridColCollect;
        /// <summary>
        /// 控件组的Y值
        /// </summary>
        private int ControlY = 0;

        /// <summary>
        /// 保存checkedit控件的选择和未选择的值
        /// </summary>
        private List<string> CheckValueList = new List<string>();

        /// <summary>
        /// 保存生成的Where条件语句
        /// </summary>
        private string StrWhere = "";
        /// <summary>
        /// 查询条件JSON字符串列表
        /// </summary>
        public List<Model.CommonQueryJson> ComQueryJsonList = new List<Model.CommonQueryJson>();

        /// <summary>
        /// 父窗体名称
        /// </summary>
        public string OwnerFormName = "";

        /// <summary>
        /// 标题列列表
        /// </summary>
        private List<Model.SysComboControl> SysCboControlList = new List<Model.SysComboControl>();

        private Dictionary<string, List<Model.SysComboControl>> DicCodeControlList = new Dictionary<string, List<Model.SysComboControl>>();


        public AdvCommonQueryDlgForm(GridColumnCollection gridColCollect)
            : base(false)
        {
            InitializeComponent();
            GridColCollect = gridColCollect;


        }
        /// <summary>
        /// 重写窗体的ShowDialog方法
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DialogResult ShowDialog(out  string strWhere)
        {
            OwnerFormName = this.Owner.Name;
            strWhere = "";
            DialogResult dresul = this.ShowDialog();
            if (dresul == DialogResult.OK)
            {
                strWhere = StrWhere;
            }
            return dresul;
        }

        private void AdvCommonQueryDlgForm_Load(object sender, EventArgs e)
        {
            //判断是否需要查询
            GridColumnCollection tmpGridColCollect = new GridColumnCollection(GridColCollect.View);
            foreach (GridColumn col in GridColCollect)
            {
                bool isRemove = false; //是否需要移除
                if (col.Visible)
                {
                    if (col.Tag != null)
                    {
                        string[] tags = col.Tag.ToString().Split(new char[1] { ';' });
                        if (tags.Length > 0)
                        {
                            if (tags[0] == "0")
                            {
                                isRemove = true;
                            }
                            else
                            {

                            }
                        }
                    }
                }
                else
                {
                    isRemove = true;
                }
                if (!isRemove)
                {
                    tmpGridColCollect.Add(col);
                }

            }
            GridColCollect = tmpGridColCollect;


            foreach (GridColumn col in GridColCollect)
            {
                //添加表格标题框

                Model.FormatType formatType = (Model.FormatType)Enum.Parse(typeof(FormatType), col.DisplayFormat.FormatType.ToString());
                //if (formatType == Model.FormatType.Custom)
                //{
                //    continue;
                //}
                //判断是否需要查询

                //if (col.Tag != null)
                //{
                //    string[] tags = col.Tag.ToString().Split(new char[1] { ';' });
                //    if (tags.Length > 0)
                //    {
                //        if (tags[0] == "0")
                //        {
                //            continue;
                //        }
                //    }
                //}

                Model.SysComboControl sysComboModel = new Model.SysComboControl();
                sysComboModel.Name = col.Caption;
                sysComboModel.Value = col.FieldName;

                sysComboModel.FormatType = formatType;

                SysCboControlList.Add(sysComboModel);

                RepositoryItem repositoryItem = col.ColumnEdit;
                if (repositoryItem != null)
                {
                    string editTypeName = repositoryItem.EditorTypeName;
                    switch (editTypeName)
                    {
                        case "LookUpEdit":
                            {
                                RepositoryItemLookUpEdit columnEditor = (RepositoryItemLookUpEdit)repositoryItem;
                                DataTable dt = new DataTable();
                                if (!(columnEditor.DataSource is DataTable))
                                {
                                    dt = Common.GlobalUtil.ListToTable((IList)columnEditor.DataSource);
                                }
                                else
                                {
                                    dt = ((DataTable)columnEditor.DataSource).Copy();
                                }
                                List<Model.SysComboControl> codeControlList = new List<Model.SysComboControl>();
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    Model.SysComboControl cobbModel = new Model.SysComboControl();
                                    cobbModel.Value = dt.Rows[i][columnEditor.ValueMember].ToString();
                                    cobbModel.Name = dt.Rows[i][columnEditor.DisplayMember].ToString();
                                    codeControlList.Add(cobbModel);
                                }
                                if (codeControlList.Count > 0)
                                {
                                    DicCodeControlList.Add(col.FieldName, codeControlList);
                                }
                                break;
                            }
                        case "CheckEdit":
                            {
                                RepositoryItemCheckEdit columnEditor = (RepositoryItemCheckEdit)repositoryItem;
                                //CheckValueList.Add(columnEditor.DisplayValueChecked);
                                //CheckValueList.Add(columnEditor.DisplayValueUnchecked);
                                CheckValueList.Add(columnEditor.ValueChecked.ToString());
                                CheckValueList.Add(columnEditor.ValueUnchecked.ToString());

                                break;
                            }
                    }
                }
            }
            if (SysCboControlList.Count > 0)
            {
                string jsonQueryString = LoadClientConfig();
                if (jsonQueryString == "")
                {
                    Model.CommonQueryJson commQueryModel = new Model.CommonQueryJson();
                    commQueryModel.FieldName = SysCboControlList[0].Value;
                    commQueryModel.FieldType = SysCboControlList[0].FormatType;
                    AddWhereControl(commQueryModel);
                }
                else
                {
                    List<Model.CommonQueryJson> commonQueryList = JsonConvert.DeserializeObject<List<Model.CommonQueryJson>>(jsonQueryString);
                    foreach (Model.CommonQueryJson commQueryModel in commonQueryList)
                    {
                        AddWhereControl(commQueryModel);
                    }
                }

            }

        }

        private string LoadClientConfig()
        {
            string jsonQueryString = "";
            string filepath = AppDomain.CurrentDomain.BaseDirectory + "ClientConfig";
            if (Directory.Exists(filepath))
            {
                Common.INIFile iniFile = new Common.INIFile(filepath + "\\query.ini");
                jsonQueryString = iniFile.IniReadValue(OwnerFormName, "value");
            }
            return jsonQueryString;

        }


        /// <summary>
        /// 添加查询控件
        /// <creator>wangl</creator>
        /// <date>2011-12-30</date> 
        /// </summary>
        private void AddWhereControl(Model.CommonQueryJson commQueryModel)
        {
            //Model.FormatType colFormatType
            int count = xtraScrollableControl1.Controls.Count;
            if (count > 0)
            {
                ControlY = xtraScrollableControl1.Controls[count - 1].Location.Y + xtraScrollableControl1.Controls[count - 1].Size.Height;
            }
            //标题下拉框
            XtraScrollableControl xtraScrLabel = new XtraScrollableControl();
            ComboBoxEdit cboColumnTitle = new ComboBoxEdit();
            foreach (Model.SysComboControl sysCboModel in SysCboControlList)
            {
                ComboBoxItemCollection colTitle = cboColumnTitle.Properties.Items;
                colTitle.BeginUpdate();
                try
                {
                    colTitle.Add(sysCboModel);

                }
                finally
                {
                    colTitle.EndUpdate();
                }
            }
            cboColumnTitle.Location = new System.Drawing.Point(0, 0);
            for (int i = 0; i < cboColumnTitle.Properties.Items.Count; i++)
            {
                Model.SysComboControl tmpModel = (Model.SysComboControl)cboColumnTitle.Properties.Items[i];
                if (tmpModel.Value == commQueryModel.FieldName)
                {
                    cboColumnTitle.SelectedIndex = i;
                    break;
                }

            }

            cboColumnTitle.Properties.ImmediatePopup = true;
            cboColumnTitle.Properties.ShowDropDown = ShowDropDown.SingleClick;
            cboColumnTitle.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            cboColumnTitle.SelectedValueChanged += new EventHandler(cboColumnTitle_SelectedValueChanged);

            xtraScrLabel.Controls.Add(cboColumnTitle);

            int pnlWith = cboColumnTitle.Size.Width + 10;
            int pnlHeight = cboColumnTitle.Size.Height + 5;
            switch (commQueryModel.FieldType)
            {
                case Model.FormatType.Numeric:
                    {
                        //数字
                        EASNumberTextEdit txtNumeric = new EASNumberTextEdit();
                        txtNumeric.Location = new System.Drawing.Point(pnlWith, 0);
                        txtNumeric.Size = new System.Drawing.Size(105, 20);
                        txtNumeric.Properties.Mask.EditMask = "";
                        txtNumeric.EditValue = commQueryModel.FieldValue1;
                        xtraScrLabel.Controls.Add(txtNumeric);
                        pnlWith += txtNumeric.Size.Width;

                        LabelControl lblSplit = new LabelControl();
                        lblSplit.Location = new System.Drawing.Point(pnlWith, 0);
                        lblSplit.Size = new System.Drawing.Size(10, 20);
                        lblSplit.Text = " - ";
                        xtraScrLabel.Controls.Add(lblSplit);
                        pnlWith += lblSplit.Size.Width;

                        txtNumeric = new EASNumberTextEdit();
                        txtNumeric.Location = new System.Drawing.Point(pnlWith, 0);
                        txtNumeric.Size = new System.Drawing.Size(105, 20);
                        txtNumeric.Properties.Mask.EditMask = "";
                        txtNumeric.EditValue = commQueryModel.FieldValue2;
                        xtraScrLabel.Controls.Add(txtNumeric);
                        pnlWith += txtNumeric.Size.Width;

                        break;
                    }
                case Model.FormatType.DateTime:
                    {
                        //日期
                        DateEdit dtpDate = new DateEdit();
                        dtpDate.Location = new System.Drawing.Point(pnlWith, 0);
                        dtpDate.Size = new System.Drawing.Size(105, 20);
                        dtpDate.Text = commQueryModel.FieldValue1;
                        dtpDate.ImeMode = ImeMode.Disable;
                        xtraScrLabel.Controls.Add(dtpDate);
                        pnlWith += dtpDate.Size.Width;

                        LabelControl lblSplit = new LabelControl();
                        lblSplit.Location = new System.Drawing.Point(pnlWith, 0);
                        lblSplit.Size = new System.Drawing.Size(10, 20);
                        lblSplit.Text = " - ";
                        xtraScrLabel.Controls.Add(lblSplit);
                        pnlWith += lblSplit.Size.Width;

                        dtpDate = new DateEdit();
                        dtpDate.Location = new System.Drawing.Point(pnlWith, 0);
                        dtpDate.Size = new System.Drawing.Size(105, 20);
                        dtpDate.Text = commQueryModel.FieldValue2;
                        dtpDate.ImeMode = ImeMode.Disable;
                        xtraScrLabel.Controls.Add(dtpDate);
                        pnlWith += dtpDate.Size.Width;
                        break;
                    }
                case Model.FormatType.None:
                    {
                        //字符
                        int titleIndex = cboColumnTitle.SelectedIndex;
                        if (titleIndex == -1)
                        {
                            titleIndex = 0;
                        }
                        GridColumn col = GridColCollect[titleIndex];
                        RepositoryItem repositoryItem = col.ColumnEdit;
                        if (repositoryItem != null)
                        {
                            string editorTypeName = repositoryItem.EditorTypeName;
                            switch (editorTypeName)
                            {
                                case "LookUpEdit":
                                    {
                                        ComboBoxEdit cboCode = new ComboBoxEdit();
                                        if (DicCodeControlList.ContainsKey(col.FieldName))
                                        {
                                            List<Model.SysComboControl> codeControlList = DicCodeControlList[col.FieldName];
                                            foreach (Model.SysComboControl sysCboModel in codeControlList)
                                            {
                                                ComboBoxItemCollection colCode = cboCode.Properties.Items;
                                                colCode.BeginUpdate();
                                                try
                                                {
                                                    colCode.Add(sysCboModel);
                                                }
                                                finally
                                                {
                                                    colCode.EndUpdate();
                                                }
                                            }
                                            cboCode.Location = new System.Drawing.Point(pnlWith, 0);
                                            for (int i = 0; i < cboCode.Properties.Items.Count; i++)
                                            {
                                                Model.SysComboControl tmpModel = (Model.SysComboControl)cboCode.Properties.Items[i];
                                                if (tmpModel.Value == commQueryModel.FieldValue1)
                                                {
                                                    cboCode.SelectedIndex = i;
                                                    break;
                                                }
                                            }

                                            cboCode.Properties.ImmediatePopup = true;
                                            cboCode.Properties.ShowDropDown = ShowDropDown.SingleClick;
                                            cboCode.Properties.AutoComplete = true;
                                            cboCode.Width = 220;
                                            xtraScrLabel.Controls.Add(cboCode);
                                            pnlWith += cboCode.Size.Width;
                                        }
                                        break;
                                    }
                                case "CheckEdit":
                                    {
                                        CheckEdit chkCode = new CheckEdit();
                                        chkCode.Location = new System.Drawing.Point(pnlWith + 110, 0);
                                        if (CheckValueList[0] == commQueryModel.FieldValue1)
                                        {
                                            chkCode.Checked = true;
                                        }
                                        else
                                        {
                                            chkCode.Checked = false;
                                        }

                                        chkCode.Text = "";
                                        xtraScrLabel.Controls.Add(chkCode);
                                        pnlWith += 220;
                                        break;
                                    }
                                case "MemoEdit":
                                    {
                                        //普通的textbox框
                                        TextEdit txtString = new TextEdit();
                                        txtString.Location = new System.Drawing.Point(pnlWith, 0);
                                        txtString.Size = new System.Drawing.Size(220, 20);
                                        txtString.Text = commQueryModel.FieldValue1;
                                        xtraScrLabel.Controls.Add(txtString);
                                        pnlWith += txtString.Size.Width;
                                        break;
                                    }
                            }
                        }
                        else
                        {
                            //普通的textbox框
                            TextEdit txtString = new TextEdit();
                            txtString.Location = new System.Drawing.Point(pnlWith, 0);
                            txtString.Size = new System.Drawing.Size(220, 20);
                            txtString.Text = commQueryModel.FieldValue1;
                            xtraScrLabel.Controls.Add(txtString);
                            pnlWith += txtString.Size.Width;

                        }
                        break;
                    }
            }
            pnlWith += 10;
            //并且或者绑定
            ComboBoxEdit cboOperation = new ComboBoxEdit();
            cboOperation.Location = new System.Drawing.Point(pnlWith, 0);

            ComboBoxItemCollection operationColl = cboOperation.Properties.Items;
            operationColl.BeginUpdate();
            try
            {
                Model.SysComboControl operationCboModel = new Model.SysComboControl();
                operationCboModel.Value = "AND";
                operationCboModel.Name = "并且";
                operationColl.Add(operationCboModel);

                operationCboModel = new Model.SysComboControl();
                operationCboModel.Value = "OR";
                operationCboModel.Name = "或者";
                operationColl.Add(operationCboModel);

            }
            finally
            {
                operationColl.EndUpdate();
            }
            cboOperation.Properties.ImmediatePopup = true;
            cboOperation.Properties.ShowDropDown = ShowDropDown.SingleClick;
            cboOperation.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            if (!string.IsNullOrEmpty(commQueryModel.OperationString))
            {
                for (int i = 0; i < operationColl.Count; i++)
                {
                    Model.SysComboControl operationCboModel = (Model.SysComboControl)operationColl[i];
                    if (operationCboModel.Value == commQueryModel.OperationString)
                    {
                        cboOperation.SelectedIndex = i;
                        break;
                    }
                }

            }
            cboOperation.SelectedValueChanged += new EventHandler(cboOperation_SelectedValueChanged);
            xtraScrLabel.Controls.Add(cboOperation);


            pnlWith += cboOperation.Size.Width;

            //删除按钮绑定
            SimpleButton btnDel = new SimpleButton();
            //pnlWith += 10;
            btnDel.Location = new System.Drawing.Point(pnlWith + 10, 0);
            btnDel.Size = new System.Drawing.Size(17, 20);
            btnDel.Text = "-";
            btnDel.Click += new EventHandler(btnDel_Click);
            xtraScrLabel.Controls.Add(btnDel);
            pnlWith += btnDel.Size.Width;
            xtraScrLabel.Location = new System.Drawing.Point(0, ControlY);

            pnlWith += 50;

            xtraScrLabel.Size = new System.Drawing.Size(pnlWith, pnlHeight);
            this.xtraScrollableControl1.Controls.Add(xtraScrLabel);

            xtraScrollableControl1.VerticalScroll.Value = xtraScrollableControl1.VerticalScroll.Maximum;
        }



        private void cboColumnTitle_SelectedValueChanged(object sender, EventArgs e)
        {
            ComboBoxEdit cboColumnTitle = (ComboBoxEdit)sender;
            XtraScrollableControl pnlControl = (XtraScrollableControl)cboColumnTitle.Parent;
            Model.SysComboControl comboModel = (Model.SysComboControl)cboColumnTitle.SelectedItem;
            if (comboModel == null)
            {
                return;
            }
            int titleIndex = cboColumnTitle.SelectedIndex;
            //保存第一个和最后两个控件
            Control controlFirst1 = pnlControl.Controls[0];
            Control controlLast1 = pnlControl.Controls[pnlControl.Controls.Count - 1];
            Control controlLast2 = pnlControl.Controls[pnlControl.Controls.Count - 2];

            int oldControlX = pnlControl.Controls[0].Location.X + pnlControl.Controls[0].Size.Width + 10;
            int oldControlY = pnlControl.Controls[1].Location.Y;

            pnlControl.Controls.Clear();

            switch (comboModel.FormatType)
            {
                case Model.FormatType.Numeric:
                    {
                        //数字
                        pnlControl.Controls.Add(controlFirst1);
                        EASNumberTextEdit txtNumeric = new EASNumberTextEdit();
                        txtNumeric.Location = new System.Drawing.Point(oldControlX, oldControlY);
                        txtNumeric.Size = new System.Drawing.Size(105, 20);
                        txtNumeric.Properties.Mask.EditMask = "";
                        pnlControl.Controls.Add(txtNumeric);
                        oldControlX += txtNumeric.Size.Width;

                        LabelControl lblSplit = new LabelControl();
                        lblSplit.Location = new System.Drawing.Point(oldControlX, oldControlY);
                        lblSplit.Size = new System.Drawing.Size(10, 20);
                        lblSplit.Text = " - ";
                        pnlControl.Controls.Add(lblSplit);
                        oldControlX += lblSplit.Size.Width;

                        txtNumeric = new EASNumberTextEdit();
                        txtNumeric.Location = new System.Drawing.Point(oldControlX, oldControlY);
                        txtNumeric.Size = new System.Drawing.Size(105, 20);
                        txtNumeric.Properties.Mask.EditMask = "";
                        pnlControl.Controls.Add(txtNumeric);
                        oldControlX += txtNumeric.Size.Width;

                        pnlControl.Controls.Add(controlLast2);
                        pnlControl.Controls.Add(controlLast1);

                        break;
                    }
                case Model.FormatType.DateTime:
                    {
                        pnlControl.Controls.Add(controlFirst1);
                        DateEdit dtpDate = new DateEdit();
                        dtpDate.Location = new System.Drawing.Point(oldControlX, oldControlY);
                        dtpDate.Size = new System.Drawing.Size(105, 20);
                        dtpDate.Text = "";
                        dtpDate.ImeMode = ImeMode.Disable;
                        pnlControl.Controls.Add(dtpDate);
                        oldControlX += dtpDate.Size.Width;

                        LabelControl lblSplit = new LabelControl();
                        lblSplit.Location = new System.Drawing.Point(oldControlX, oldControlY);
                        lblSplit.Size = new System.Drawing.Size(10, 20);
                        lblSplit.Text = " - ";
                        pnlControl.Controls.Add(lblSplit);
                        oldControlX += lblSplit.Size.Width;

                        dtpDate = new DateEdit();
                        dtpDate.Location = new System.Drawing.Point(oldControlX, oldControlY);
                        dtpDate.Size = new System.Drawing.Size(105, 20);
                        dtpDate.Text = "";
                        dtpDate.ImeMode = ImeMode.Disable;
                        pnlControl.Controls.Add(dtpDate);
                        oldControlX += dtpDate.Size.Width;

                        pnlControl.Controls.Add(controlLast2);
                        pnlControl.Controls.Add(controlLast1);

                        break;
                    }

                case Model.FormatType.None:
                    {
                        GridColumn col = GridColCollect[titleIndex];
                        RepositoryItem repositoryItem = col.ColumnEdit;
                        if (repositoryItem != null)
                        {
                            string editorTypeName = repositoryItem.EditorTypeName;
                            switch (editorTypeName)
                            {
                                case "LookUpEdit":
                                    {
                                        ComboBoxEdit cboCode = new ComboBoxEdit();
                                        if (DicCodeControlList.ContainsKey(col.FieldName))
                                        {
                                            List<Model.SysComboControl> codeControlList = DicCodeControlList[col.FieldName];
                                            foreach (Model.SysComboControl sysCboModel in codeControlList)
                                            {
                                                ComboBoxItemCollection colTitle = cboCode.Properties.Items;
                                                colTitle.BeginUpdate();
                                                try
                                                {
                                                    colTitle.Add(sysCboModel);

                                                }
                                                finally
                                                {
                                                    colTitle.EndUpdate();
                                                }
                                            }
                                            cboCode.Location = new System.Drawing.Point(oldControlX, oldControlY);
                                            cboCode.SelectedIndex = 0;
                                            cboCode.Properties.ImmediatePopup = true;
                                            cboCode.Properties.ShowDropDown = ShowDropDown.SingleClick;
                                            cboCode.Properties.AutoComplete = true;
                                            cboCode.Width = 220;
                                            oldControlX += cboCode.Size.Width;
                                            pnlControl.Controls.Add(controlFirst1);
                                            pnlControl.Controls.Add(cboCode);
                                            pnlControl.Controls.Add(controlLast2);
                                            pnlControl.Controls.Add(controlLast1);
                                        }
                                        break;
                                    }
                                case "CheckEdit":
                                    {
                                        CheckEdit chkCode = new CheckEdit();
                                        chkCode.Location = new System.Drawing.Point(oldControlX + 115, oldControlY);
                                        chkCode.Checked = true;
                                        chkCode.Text = "";
                                        oldControlX += 220;
                                        pnlControl.Controls.Add(controlFirst1);
                                        pnlControl.Controls.Add(chkCode);
                                        pnlControl.Controls.Add(controlLast2);
                                        pnlControl.Controls.Add(controlLast1);
                                        break;
                                    }
                                case "MemoEdit":
                                    {
                                        TextEdit txtString = new TextEdit();
                                        txtString.Location = new System.Drawing.Point(oldControlX, oldControlY);
                                        txtString.Size = new System.Drawing.Size(220, 20);
                                        oldControlX += 220;
                                        pnlControl.Controls.Add(controlFirst1);
                                        pnlControl.Controls.Add(txtString);
                                        pnlControl.Controls.Add(controlLast2);
                                        pnlControl.Controls.Add(controlLast1);
                                        break;
                                    }

                            }
                        }
                        else
                        {
                            //普通的textbox框
                            TextEdit txtString = new TextEdit();
                            txtString.Location = new System.Drawing.Point(oldControlX, 0);
                            txtString.Size = new System.Drawing.Size(220, 20);
                            oldControlX += txtString.Size.Width;
                            pnlControl.Controls.Add(controlFirst1);
                            pnlControl.Controls.Add(txtString);
                            pnlControl.Controls.Add(controlLast2);
                            pnlControl.Controls.Add(controlLast1);

                        }

                        break;
                    }

            }
        }

        private void cboOperation_SelectedValueChanged(object sender, EventArgs e)
        {
            ComboBoxEdit cboOperation = (ComboBoxEdit)sender;
            if (cboOperation.SelectedIndex != -1)
            {
                XtraScrollableControl pnlControl = (XtraScrollableControl)cboOperation.Parent;
                int index = xtraScrollableControl1.Controls.IndexOf(pnlControl);
                if (xtraScrollableControl1.Controls.Count <= (index + 1))
                {
                    ComboBoxEdit cboColumnTitle = (ComboBoxEdit)pnlControl.Controls[0];
                    //Model.SysComboControl comboModel = (Model.SysComboControl)SysCboControlList[0];

                    Model.CommonQueryJson commQueryModel = new Model.CommonQueryJson();
                    commQueryModel.FieldName = SysCboControlList[0].Value;
                    commQueryModel.FieldType = SysCboControlList[0].FormatType;
                    AddWhereControl(commQueryModel);
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (xtraScrollableControl1.Controls.Count == 1)
            {
                //只有一行
                ClearControls();
            }
            else
            {
                XtraScrollableControl pnlControl = (XtraScrollableControl)((SimpleButton)sender).Parent;


                int index = xtraScrollableControl1.Controls.IndexOf(pnlControl);

                if (index == (xtraScrollableControl1.Controls.Count - 1))
                {
                    XtraScrollableControl pnlControl1 = (XtraScrollableControl)xtraScrollableControl1.Controls[index - 1];
                    ComboBoxEdit cboOperation = (ComboBoxEdit)pnlControl1.Controls[pnlControl1.Controls.Count - 2];
                    cboOperation.SelectedIndex = -1;
                }

                xtraScrollableControl1.Controls.Remove(pnlControl);



                ControlY = 0 - xtraScrollableControl1.VerticalScroll.Value;

                foreach (XtraScrollableControl pnl in xtraScrollableControl1.Controls)
                {
                    pnl.Location = new System.Drawing.Point(0, ControlY);
                    ControlY = pnl.Location.Y + pnl.Size.Height;
                }


            }

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strWhere = " ";
            int index = 0;
            foreach (XtraScrollableControl pnlControl in xtraScrollableControl1.Controls)
            {
                //第一个控件,列名称
                ComboBoxEdit cboColumnTitle = (ComboBoxEdit)pnlControl.Controls[0];


                int titleIndex = cboColumnTitle.SelectedIndex;
                Model.SysComboControl sysComboModel = (Model.SysComboControl)cboColumnTitle.SelectedItem;
                string filedName = sysComboModel.Value;
                string oprationString = "";
                string nowOprationString = "";

                if (index == xtraScrollableControl1.Controls.Count - 1)
                {
                }
                else
                {
                    //当前控件，不是最后一个控件
                    XtraScrollableControl nowPnlControl = (XtraScrollableControl)xtraScrollableControl1.Controls[index];
                    ComboBoxEdit nowLboOperation = (ComboBoxEdit)nowPnlControl.Controls[nowPnlControl.Controls.Count - 2]; //当前控件的操作下拉框
                    Model.SysComboControl nowOpComboModel = (Model.SysComboControl)nowLboOperation.SelectedItem;
                    if (nowOpComboModel != null)
                    {
                        nowOprationString = nowOpComboModel.Value;
                    }
                }



                if (index > 0)
                {
                    //上一组控件
                    XtraScrollableControl lastPnlControl = (XtraScrollableControl)xtraScrollableControl1.Controls[index - 1];
                    ComboBoxEdit cboOperation = (ComboBoxEdit)lastPnlControl.Controls[lastPnlControl.Controls.Count - 2];
                    Model.SysComboControl opComboModel = (Model.SysComboControl)cboOperation.SelectedItem;
                    oprationString = " " + opComboModel.Value + " ";


                }
                if (titleIndex != -1)
                {
                    //操作符的下拉框

                    switch (sysComboModel.FormatType)
                    {
                        case Model.FormatType.None:
                            {
                                //字符
                                GridColumn col = GridColCollect[titleIndex];
                                RepositoryItem repositoryItem = col.ColumnEdit;
                                if (repositoryItem != null)
                                {
                                    string editorTypeName = repositoryItem.EditorTypeName;
                                    switch (editorTypeName)
                                    {
                                        case "LookUpEdit":
                                            {
                                                ComboBoxEdit cboCode = (ComboBoxEdit)pnlControl.Controls[1];
                                                if (cboCode.SelectedItem != null)
                                                {
                                                    if (cboCode.SelectedItem.ToString() != "")
                                                    {
                                                        Model.SysComboControl model = (Model.SysComboControl)cboCode.SelectedItem;
                                                        if (cboCode.SelectedIndex != -1)
                                                        {
                                                            strWhere += oprationString + filedName + "='" + model.Value + "'";

                                                            Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                                            comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                                            comQueryJsonModel.ControlType = "LookUpEdit";
                                                            comQueryJsonModel.FieldName = sysComboModel.Value;
                                                            comQueryJsonModel.FieldValue1 = model.Value;
                                                            comQueryJsonModel.OperationString = nowOprationString;
                                                            ComQueryJsonList.Add(comQueryJsonModel);
                                                        }
                                                    }
                                                }

                                                break;
                                            }
                                        case "CheckEdit":
                                            {
                                                CheckEdit chkCode = (CheckEdit)pnlControl.Controls[1];
                                                Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                                comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                                comQueryJsonModel.ControlType = "CheckEdit";
                                                comQueryJsonModel.FieldName = sysComboModel.Value;
                                                comQueryJsonModel.OperationString = nowOprationString;
                                                if (chkCode.Checked)
                                                {
                                                    if (filedName == "pics" || filedName == "ga_pics")//房源图
                                                    {
                                                        strWhere += oprationString + filedName + ">0";
                                                    }
                                                    else
                                                    {
                                                        strWhere += oprationString + filedName + "='" + CheckValueList[0] + "'";
                                                    }

                                                    comQueryJsonModel.FieldValue1 = CheckValueList[0];
                                                }
                                                else
                                                {
                                                    if (filedName == "pics" || filedName == "ga_pics")//房源图
                                                    {
                                                        strWhere += oprationString + filedName + "<=0";
                                                    }
                                                    else
                                                    {
                                                        strWhere += oprationString + filedName + "='" + CheckValueList[1] + "'";
                                                    }

                                                    comQueryJsonModel.FieldValue1 = CheckValueList[1];
                                                }
                                                ComQueryJsonList.Add(comQueryJsonModel);

                                                break;
                                            }
                                        case "MemoEdit":
                                            {
                                                TextEdit txtString = (TextEdit)pnlControl.Controls[1]; ;
                                                if (txtString.Text.Trim() != "")
                                                {
                                                    if (Fetch.InspectData(txtString.Text, CheckGetEnum.Safety) == string.Empty)
                                                    {

                                                    }
                                                    else
                                                    {
                                                        strWhere += oprationString + filedName + " LIKE '%" + txtString.Text.Trim() + "%'";
                                                        Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                                        comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                                        comQueryJsonModel.ControlType = "None";
                                                        comQueryJsonModel.FieldName = sysComboModel.Value;
                                                        comQueryJsonModel.FieldValue1 = txtString.Text.Trim();
                                                        comQueryJsonModel.OperationString = nowOprationString;
                                                        ComQueryJsonList.Add(comQueryJsonModel);
                                                    }
                                                }
                                                break;
                                            }
                                    }
                                }
                                else
                                {
                                    TextEdit txtString = (TextEdit)pnlControl.Controls[1]; ;
                                    if (txtString.Text.Trim() != "")
                                    {
                                        if (Fetch.InspectData(txtString.Text, CheckGetEnum.Safety) == string.Empty)
                                        {

                                        }
                                        else
                                        {
                                            strWhere += oprationString + filedName + " LIKE '%" + txtString.Text.Trim() + "%'";
                                            Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                            comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                            comQueryJsonModel.ControlType = "None";
                                            comQueryJsonModel.FieldName = sysComboModel.Value;
                                            comQueryJsonModel.FieldValue1 = txtString.Text.Trim();
                                            comQueryJsonModel.OperationString = nowOprationString;
                                            ComQueryJsonList.Add(comQueryJsonModel);
                                        }
                                    }
                                }
                                break;
                            }
                        case Model.FormatType.DateTime:
                            {
                                //日期
                                DateEdit dtpStart = (DateEdit)pnlControl.Controls[1];
                                DateEdit dtpEnd = (DateEdit)pnlControl.Controls[3];
                                if (dtpStart.Text != "" && dtpEnd.Text == "")
                                {
                                    strWhere += oprationString + filedName + ">='" + dtpStart.Text + "'";
                                    Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                    comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                    comQueryJsonModel.ControlType = "None";
                                    comQueryJsonModel.FieldName = sysComboModel.Value;
                                    comQueryJsonModel.FieldValue1 = dtpStart.Text.Trim();
                                    comQueryJsonModel.OperationString = nowOprationString;
                                    ComQueryJsonList.Add(comQueryJsonModel);

                                }
                                else
                                {
                                    if (dtpStart.Text == "" && dtpEnd.Text != "")
                                    {
                                        strWhere += oprationString + filedName + "<='" + dtpEnd.Text + "'";
                                        Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                        comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                        comQueryJsonModel.ControlType = "None";
                                        comQueryJsonModel.FieldName = sysComboModel.Value;
                                        comQueryJsonModel.FieldValue2 = dtpEnd.Text.Trim();
                                        comQueryJsonModel.OperationString = nowOprationString;
                                        ComQueryJsonList.Add(comQueryJsonModel);
                                    }
                                    else
                                    {
                                        if (dtpStart.Text != "" && dtpEnd.Text != "")
                                        {
                                            strWhere += oprationString + " (DATEDIFF(day,'" + dtpStart.Text + "'," + filedName + ")>=0 AND ";
                                            strWhere += " DATEDIFF(day,'" + dtpEnd.Text + "'," + filedName + ")<=0)";
                                            Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                            comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                            comQueryJsonModel.ControlType = "None";
                                            comQueryJsonModel.FieldName = sysComboModel.Value;
                                            comQueryJsonModel.FieldValue1 = dtpStart.Text.Trim();
                                            comQueryJsonModel.FieldValue2 = dtpEnd.Text.Trim();
                                            comQueryJsonModel.OperationString = nowOprationString;
                                            ComQueryJsonList.Add(comQueryJsonModel);
                                        }
                                    }
                                }
                                break;
                            }
                        case Model.FormatType.Numeric:
                            {
                                //数字
                                EASNumberTextEdit txtNumericStart = (EASNumberTextEdit)pnlControl.Controls[1];
                                EASNumberTextEdit txtNumericEnd = (EASNumberTextEdit)pnlControl.Controls[3];
                                string numericStartValue = txtNumericStart.EditValue == null ? "" : txtNumericStart.EditValue.ToString().Trim();
                                string numericEndValue = txtNumericEnd.EditValue == null ? "" : txtNumericEnd.EditValue.ToString().Trim();
                                if (numericStartValue != "" && numericEndValue == "")
                                {
                                    strWhere += oprationString + filedName + ">=" + numericStartValue;
                                    Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                    comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                    comQueryJsonModel.ControlType = "None";
                                    comQueryJsonModel.FieldName = sysComboModel.Value;
                                    comQueryJsonModel.FieldValue1 = numericStartValue;
                                    comQueryJsonModel.OperationString = nowOprationString;
                                    ComQueryJsonList.Add(comQueryJsonModel);

                                }
                                else
                                {
                                    if (numericStartValue == "" && numericEndValue != "")
                                    {
                                        strWhere += oprationString + filedName + "<=" + numericEndValue;
                                        Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                        comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                        comQueryJsonModel.ControlType = "None";
                                        comQueryJsonModel.FieldName = sysComboModel.Value;
                                        comQueryJsonModel.FieldValue2 = numericEndValue;
                                        comQueryJsonModel.OperationString = nowOprationString;
                                        ComQueryJsonList.Add(comQueryJsonModel);

                                    }
                                    else
                                    {
                                        if (numericStartValue != "" && numericEndValue != "")
                                        {
                                            strWhere += oprationString + filedName + " BETWEEN " + numericStartValue + " AND " + numericEndValue;
                                            Model.CommonQueryJson comQueryJsonModel = new Model.CommonQueryJson();
                                            comQueryJsonModel.FieldType = sysComboModel.FormatType;
                                            comQueryJsonModel.ControlType = "None";
                                            comQueryJsonModel.FieldName = sysComboModel.Value;
                                            comQueryJsonModel.FieldValue1 = numericStartValue;
                                            comQueryJsonModel.FieldValue2 = numericEndValue;
                                            comQueryJsonModel.OperationString = nowOprationString;
                                            ComQueryJsonList.Add(comQueryJsonModel);
                                        }
                                    }
                                }
                                break;
                            }
                    }

                }
                index++;
            }


            StrWhere = strWhere.Trim();
            if (StrWhere.IndexOf("AND") == 0)
            {

                StrWhere = StrWhere.Substring(3);
            }
            else
            {
                if (StrWhere.IndexOf("OR") == 0)
                {
                    StrWhere = StrWhere.Substring(2);
                }
            }
            string filepath = AppDomain.CurrentDomain.BaseDirectory + "ClientConfig";
            Common.INIFile iniFile = new Common.INIFile(filepath + "\\query.ini");
            if (StrWhere != "" && OwnerFormName != "" && ComQueryJsonList.Count > 0)
            {
                ComQueryJsonList[ComQueryJsonList.Count - 1].OperationString = "";
                string jsonStr = JsonConvert.SerializeObject(ComQueryJsonList);
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }

                iniFile.IniWriteValue(OwnerFormName, "value", jsonStr);

            }
            else
            {
                iniFile.ClearSection(OwnerFormName);
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        /// <summary>
        /// 清除控件
        /// <creator>wangl</creator>
        /// <date>2012-01-04</date>
        /// </summary>
        private void ClearControls()
        {
            xtraScrollableControl1.Controls.Clear();
            ControlY = 0;
            Model.CommonQueryJson commQueryModel = new Model.CommonQueryJson();
            commQueryModel.FieldName = SysCboControlList[0].Value;
            commQueryModel.FieldType = SysCboControlList[0].FormatType;
            AddWhereControl(commQueryModel);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearControls();
        }

    }
}
